package com.jvyou.app.ds.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jvyou.app.common.annotation.AutoDict;
import com.jvyou.app.common.domain.dto.UserDto;
import com.jvyou.app.common.domain.entity.User;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.domain.vo.UserVo;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.common.utils.value.ValueChecker;
import com.jvyou.app.ds.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户表(User)表控制层
 *
 * @author 橘柚
 * @since 2025-03-04 08:58:55
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 查询全部用户
     *
     * @return 列表
     */
    @GetMapping("/list")
    @SaCheckPermission("system:user:list")
    public Result<List<UserVo>> listUser(@ModelAttribute UserDto userDto) {
        return Result.success(userService.listUser(userDto));
    }


    /**
     * 分页查询用户信息
     *
     * @return 列表
     */
    @GetMapping("/page")
    @AutoDict
    @SaCheckPermission("system:user:page")
    public Result<PageResult<UserVo>> pageUser(@ModelAttribute UserDto userDto) {
        return Result.success(userService.pageUser(userDto));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @SaCheckPermission("system:user:get")
    public Result<User> selectOne(@PathVariable Long id) {
        return Result.success(userService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体对象
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("system:user:add")
    public Result<Boolean> insert(@RequestBody User user) {
        ValueChecker.checkNotEmpty(user.getPhone(), "手机号不能为空");
        ValueChecker.checkNotEmpty(user.getUsername(), "用户名不能为空");
        return userService.addUser(user);
    }

    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("system:user:update")
    public Result<User> updateById(@RequestBody User user) {
        boolean success = userService.updateById(user);
        return success ? Result.success(user) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }


    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:user:delete")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = userService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }

    /**
     * 修改用户信息
     *
     * @param user 实体对象
     * @return 删除结果
     */
    @PutMapping("/updateUserInfo")
    @SaCheckPermission("system:user:updateUserInfo")
    public Result<Boolean> update(@RequestBody User user) {
        return Result.success(userService.updateUserInfo(user));
    }

}

